﻿using Kimd.Common;
using Kimd.Communication;
using Kimd.Logic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WorkStation.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WorkStation.Logic
{
    public class TesterStatusLogic : FlowBase
    {
        private int _testerIndex = 0;
        private short[] _inStatus = null;
        private short[] _inStatus2 = null;
        private ModbusTcp _modbusOperator = null;
        private string _ipAddress;

        public TesterStatusLogic(string flowName, FlowManager flowmanager, int testerIndex, string ipAddress) 
            : base(flowName, flowmanager)
        {
            this._testerIndex = testerIndex;
            this._ipAddress = ipAddress;
            this._modbusOperator = _testerIndex == 0 ? GlobalVariable.Tester1PLC : GlobalVariable.Tester2PLC;
        }

        public override void Calibrate()
        {
            base.Calibrate();
        }

        public override void Home()
        {
            base.Home();
        }

        public override void Init(out string errInfo)
        {
            base.Init(out errInfo);
        }

        public override void InSingleRunMode()
        {
            base.InSingleRunMode();
        }

        public override void Method()
        {
            return;

            if (_modbusOperator == null)
            {
                _modbusOperator = new ModbusTcp(_ipAddress, 502);
            reconnect:
                if (!_modbusOperator.Connect())
                {
                    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Warn, $"[测试治具{_testerIndex + 1}]连接失败!", this.Description, 7, "轨道"));
                    //出错，停下所有线程
                    _flowManager.Pause();
                    //若为报错，需重新检测启动信号
                    WaitForStart();
                    if (!CheckFlowCondition()) return;
                    goto reconnect;
                }
            }
            if (_modbusOperator.IsConnected)
            {
                //读
                _modbusOperator.ReadShort("34288", 10, out _inStatus);
                _modbusOperator.ReadShort("34308", 1, out _inStatus2);
                //写
                if (_testerIndex == 0)
                {
                    _modbusOperator.WriteShort("34388", new short[] { GlobalVariable.HandlerControlData[0] });
                    _modbusOperator.WriteShort("34389", new short[] { GlobalVariable.HandlerActionData[0] });
                    _modbusOperator.WriteShort("34390", new short[] { GlobalVariable.HandlerTesterCellData1[0] });
                    _modbusOperator.WriteShort("34392", new short[] { GlobalVariable.HandlerTesterCellData2[0] });
                    _modbusOperator.WriteShort("34394", new short[] { GlobalVariable.HandlerTesterCellData3[0] });
                    _modbusOperator.WriteShort("34396", new short[] { GlobalVariable.HandlerTesterCellData4[0] });
                }
                else
                {
                    _modbusOperator.WriteShort("34388", new short[] { GlobalVariable.HandlerControlData[1] });
                    _modbusOperator.WriteShort("34389", new short[] { GlobalVariable.HandlerActionData[1] });
                    _modbusOperator.WriteShort("34390", new short[] { GlobalVariable.HandlerTesterCellData1[1] });
                    _modbusOperator.WriteShort("34392", new short[] { GlobalVariable.HandlerTesterCellData2[1] });
                    _modbusOperator.WriteShort("34394", new short[] { GlobalVariable.HandlerTesterCellData3[1] });
                    _modbusOperator.WriteShort("34396", new short[] { GlobalVariable.HandlerTesterCellData4[1] });
                }
                Thread.Sleep(100);
            }
        }

        public override void OnStop()
        {
            base.OnStop();
        }

        public override bool PreStartCheck()
        {
            return base.PreStartCheck();
        }

        public override void SpotCheck()
        {
            base.SpotCheck();
        }
        public bool IsInStatus(TesterStatusInFlag status)
        {
            if (_inStatus == null)
                return false;
            return ((_inStatus[0] >> (int)status) & 0x01) == 1;
        }
        public bool IsInAction(TesterActionInFlag action)
        {
            if (_inStatus == null)
                return false;
            return ((_inStatus[1] >> (int)action) & 0x01) == 1;
        }
        public bool IsAlarm()
        {
            return _inStatus2[0] != 0;
        }
        public TesterCellScanInFlag GetCellScanStatus(int cellIndex)
        {
            if (_inStatus == null)
                return TesterCellScanInFlag.无数据;
            return (TesterCellScanInFlag)_inStatus[2 + 2 * cellIndex];
        }
        public TesterCellResultInFlag GetCellResultStatus(int cellIndex)
        {
            if (_inStatus == null)
                return TesterCellResultInFlag.无数据;
            return (TesterCellResultInFlag)_inStatus[3 + 2 * cellIndex];
        }
    }
    public enum TesterStatusInFlag
    {
        [Description("转盘运行中")]
        转盘运行中,
        [Description("转盘停止中")]
        转盘停止中,
        [Description("转盘急停中")]
        转盘急停中,
        [Description("转盘允许启动")]
        转盘允许启动,
        [Description("转盘初始化完成")]
        转盘初始化完成,
        [Description("转盘初始化中")]
        转盘初始化中,
        [Description("转盘报警中")]
        转盘报警中 = 7,
        [Description("转盘清料完成")]
        转盘清料完成,
        [Description("转盘需要进行测试样品")]
        转盘需要进行测试样品 = 10,
    }
    public enum TesterActionInFlag
    {
        [Description("转盘允许放料")]
        转盘允许放料,
        [Description("放料完成转盘动作")]
        放料完成转盘动作,
        [Description("转盘允许取料")]
        转盘允许取料,
    }
    public enum TesterCellScanInFlag
    {
        [Description("无产品")]
        无产品,
        [Description("条码OK")]
        条码OK,
        [Description("条码格式错误")]
        条码格式错误,
        [Description("流程错误")]
        流程错误,
        [Description("AAA错误")]
        AAA错误,
        [Description("扫码异常")]
        扫码异常,
        [Description("无数据")]
        无数据,
    }
    public enum TesterCellResultInFlag
    {
        [Description("无产品")]
        无产品,
        [Description("测试PASS")]
        测试PASS,
        [Description("测试FAIL")]
        测试FAIL,
        [Description("无数据")]
        无数据,
    }
    public enum TesterAlarmInFlag
    {
        [Description("转盘报警1(气缸1报警)")]
        转盘报警1,
        [Description("转盘报警2(气缸2报警)")]
        转盘报警2,
        [Description("转盘报警3(气缸3报警)")]
        转盘报警3,
        [Description("转盘报警4(气缸4报警)")]
        转盘报警4,
        [Description("转盘报警5(气缸5报警)")]
        转盘报警5,
        [Description("转盘报警6(气缸6报警)")]
        转盘报警6,
        [Description("转盘报警7(气缸7报警)")]
        转盘报警7,
        [Description("转盘报警8(气缸8报警)")]
        转盘报警8,
        [Description("转盘报警9(气缸9报警)")]
        转盘报警9,
        [Description("转盘报警10(气缸10报警)")]
        转盘报警10,
        [Description("转盘报警11(气缸11报警)")]
        转盘报警11,
        [Description("转盘报警12(气缸12报警)")]
        转盘报警12,
        [Description("转盘报警13(气缸13报警)")]
        转盘报警13,
        [Description("转盘报警14(气缸14报警)")]
        转盘报警14,
        [Description("转盘报警15(气缸15报警)")]
        转盘报警15,
        [Description("转盘报警16(气缸16报警)")]
        转盘报警16,
    }
    public enum HandlerControlOutFlag
    {
        [Description("上下料机启动按钮")]
        上下料机启动按钮,
        [Description("上下料机停止按钮")]
        上下料机停止按钮,
        [Description("上下料机急停按钮")]
        上下料机急停按钮,
        [Description("上下料机复位按钮")]
        上下料机复位按钮,
        [Description("上下料机初始化按钮")]
        上下料机初始化按钮,
        [Description("上下料机报警中")]
        上下料机报警中 = 7,
        [Description("上料机进行清料中")]
        上料机进行清料中,
        [Description("上下料机正在进行样品验证")]
        上下料机正在进行样品验证 = 11,
    }
    public enum HandlerActionOutFlag
    {
        [Description("上下料机放料到位")]
        上下料机放料到位,
        [Description("上下料机放料完成允许转盘动作")]
        上下料机放料完成允许转盘动作,
        [Description("上下料机取料完成")]
        上下料机取料完成,
    }
    public enum HandlerPutOutFlag
    {
        [Description("无产品")]
        无产品,
        [Description("有产品")]
        有产品,
    }
}
